package org.jeecg.modules.controller.printapply;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.bean.entity.printapply.TbPrintApply;
import org.jeecg.modules.mapper.printapply.TbPrintApplyMapper;
import org.jeecg.modules.service.printapply.ITbPrintApplyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
 * @Description: 病历复印申请表
 * @Author: jeecg-boot
 * @Date: 2023-11-29
 * @Version: V1.0
 */
@Api(tags = "病历复印申请表")
@RestController
@RequestMapping("/printapply/tbPrintApply")
@Slf4j
public class TbPrintApplyController extends JeecgController<TbPrintApply, ITbPrintApplyService> {
    @Autowired
    private ITbPrintApplyService tbPrintApplyService;

    @Autowired
    private TbPrintApplyMapper applyMapper;

    /**
     * 分页列表查询
     *
     * @param tbPrintApply
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    //@AutoLog(value = "病历复印申请表-分页列表查询")
    @ApiOperation(value = "病历复印申请表-分页列表查询", notes = "病历复印申请表-分页列表查询")
    @GetMapping(value = "/list")
    public Result<IPage<TbPrintApply>> queryPageList(TbPrintApply tbPrintApply,
                                                     @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                     @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                     HttpServletRequest req) {
        QueryWrapper<TbPrintApply> queryWrapper = QueryGenerator.initQueryWrapper(tbPrintApply, req.getParameterMap());
        Page<TbPrintApply> page = new Page<>(pageNo, pageSize);
        IPage<TbPrintApply> applyIPage = applyMapper.getApplyListPage(page, queryWrapper);
        return Result.OK(applyIPage);
    }

    @GetMapping("/getApplyListByItems")
    public Result<IPage<TbPrintApply>> getApplyList(TbPrintApply tbPrintApply,
                                                    @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                    @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                    HttpServletRequest req) {
        Page<TbPrintApply> page = new Page<>(pageNo, pageSize);
        String startTime = null;
        String endTime = null;
        if (null != req.getParameterMap().get("createTime_begin") && null != req.getParameterMap().get("createTime_end")) {
            String[] createTimeBegins = req.getParameterMap().get("createTime_begin");
            startTime = createTimeBegins[0];
            String[] createTimeEnds = req.getParameterMap().get("createTime_end");
            endTime = createTimeEnds[0];
        }
        IPage<TbPrintApply> applyListInfo = tbPrintApplyService.getApplyListInfo(page, tbPrintApply, startTime, endTime);
        return Result.OK(applyListInfo);
    }

    /**
     * 添加
     *
     * @param tbPrintApply
     * @return
     */
    @AutoLog(value = "病历复印申请表-添加")
    @ApiOperation(value = "病历复印申请表-添加", notes = "病历复印申请表-添加")
//    @RequiresPermissions("printapply:tb_print_apply:add")
    @PostMapping(value = "/add")
    public Result<String> add(@RequestBody TbPrintApply tbPrintApply) {
        if (!oConvertUtils.isEmpty(tbPrintApply.getIdNum())) {
            tbPrintApplyService.save(tbPrintApply);
            return Result.OK("添加成功！");
        } else {
            return Result.error("数据不能为空");
        }
    }

    /**
     * 编辑
     *
     * @param tbPrintApply
     * @return
     */
    @AutoLog(value = "病历复印申请表-编辑")
    @ApiOperation(value = "病历复印申请表-编辑", notes = "病历复印申请表-编辑")
//    @RequiresPermissions("printapply:tb_print_apply:edit")
    @RequestMapping(value = "/edit", method = {RequestMethod.PUT, RequestMethod.POST})
    public Result<String> edit(@RequestBody TbPrintApply tbPrintApply) {
        tbPrintApplyService.updateById(tbPrintApply);
        return Result.OK("编辑成功!");
    }

    @AutoLog(value = "病历复印申请表-审核状态")
    @ApiOperation(value = "病历复印申请表-审核状态", notes = "病历复印申请表-审核状态")
    @PutMapping(value = "/updateApplyStatusById")
    public Result<String> updateApplyStatusById(@RequestBody TbPrintApply printApply) {
        tbPrintApplyService.updateStatusById(printApply);
        return Result.ok("审核成功");
    }


    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "病历复印申请表-通过id删除")
    @ApiOperation(value = "病历复印申请表-通过id删除", notes = "病历复印申请表-通过id删除")
    @RequiresPermissions("printapply:tb_print_apply:delete")
    @DeleteMapping(value = "/delete")
    public Result<String> delete(@RequestParam(name = "id", required = true) String id) {
        tbPrintApplyService.removeById(id);
        return Result.OK("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "病历复印申请表-批量删除")
    @ApiOperation(value = "病历复印申请表-批量删除", notes = "病历复印申请表-批量删除")
    @RequiresPermissions("printapply:tb_print_apply:deleteBatch")
    @DeleteMapping(value = "/deleteBatch")
    public Result<String> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.tbPrintApplyService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    //@AutoLog(value = "病历复印申请表-通过id查询")
    @ApiOperation(value = "病历复印申请表-通过id查询", notes = "病历复印申请表-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<TbPrintApply> queryById(@RequestParam(name = "id", required = true) String id) {
        TbPrintApply tbPrintApply = tbPrintApplyService.getById(id);
        if (tbPrintApply == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(tbPrintApply);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param tbPrintApply
     */
    @RequiresPermissions("printapply:tb_print_apply:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, TbPrintApply tbPrintApply) {
        return super.exportXls(request, tbPrintApply, TbPrintApply.class, "病历复印申请表");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    @RequiresPermissions("printapply:tb_print_apply:importExcel")
    @PostMapping(value = "/importExcel")
    public Result<String> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, TbPrintApply.class);
    }

}
